// import { userLogin } from '../services/user';

import {
  createWork, deleteWork, updateWork, findWork, findWorkMany, endWork, createWorkTimer,
  deleteWorkTimer, updateWorkTimer, findWorkTimer, findWorkTimerMany
} from '../services/work';
import {taxAssess} from "../services/finance";
import moment from "moment";


export default {
  namespace: 'work',
  state: {},
  effects: {

    * createWork({payload}, {call, put}) {
      const response = yield call(createWork, payload);

      if (response) {
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * deleteWork({payload}, {call, put}) {
      const response = yield call(deleteWork, payload);
      if (response) {
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * updateWork({payload}, {call, put}) {
      const response = yield call(updateWork, payload);

      if (response) {
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * findWork({payload}, {call, put}) {
      //需要先清除原来的数据
      yield put({
        type: 'onDataLoad',
        payload: {work: {workList: []}}
      });
      const {code} = payload;
      console.log('find work', code);
      if (!code) {
        yield put({
          type: 'onDataLoad',
          payload: {work: {code: "WRK" + moment().format("YYYYMMDDHHmmss"), workList: []}}
        });
        return;
      }
      const response = yield call(findWork, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {work: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response.result;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * findWorkMany({payload}, {call, put}) {
      const response = yield call(findWorkMany, payload);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {works: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response.result;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * endWork({payload}, {call, put}) {
      const response = yield call(endWork, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {endWork: result}});
      return response;
    },


    * createWorkTimer({payload}, {call, put}) {
      const response = yield call(createWorkTimer, payload);

      if (response) {
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * deleteWorkTimer({payload}, {call, put}) {
      const response = yield call(deleteWorkTimer, payload);
      if (response) {
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * updateWorkTimer({payload}, {call, put}) {
      const response = yield call(updateWorkTimer, payload);
      if (response) {
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "操作失败，请检查网络！", type: 'error'}
      });
    },
    * findWorkTimer({payload}, {call, put}) {
      const response = yield call(findWorkTimer, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "操作失败，请检查网络！", type: 'error'}});
        return;
      }
      const {status, result, message = "操作失败，请检查网络！"} = response;

      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return response;
      }
      yield put({type: 'onDataLoad', payload: {result: {workTimers: result}}});
      yield put({type: 'onNotify', payload: {message: "操作成功！", type: 'info'}});
    },

    * findWorkTimerMany({payload}, {call, put}) {
      const response = yield call(findWorkTimerMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "操作失败，请检查网络！", type: 'error'}});
        return;
      }
      const {status, result=[], message = "操作失败，请检查网络！"} = response;

      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return response;
      }
      yield put({type: 'onDataLoad', payload: {workTimers: result}});
      yield put({type: 'onNotify', payload: {message: "操作成功！", type: 'info'}});
    }
  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    }

  }
};
